﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Marmat.Template.WinForm
{
    internal partial class MyTextBox : UserControl
    {
        public MyTextBox()
        {
            InitializeComponent();
            this.textBox1.GotFocus += TextBox1_GotFocus;
            this.textBox1.LostFocus += TextBox1_LostFocus;
        }


        [Description("编辑框的文本")]
        [DefaultValue("")]
        public string Value
        {
            get => this.textBox1.Text;
            set => this.textBox1.Text = value;
        }

        [Description("获取或设置用于对单个行中的密码掩码字符的字符 System.Windows.Forms.TextBox 控件。")]
        public char PasswordChar
        {
            get => this.textBox1.PasswordChar;
            set => this.textBox1.PasswordChar = value;
        }

        [Description("编辑框下划线焦点色")]
        [DefaultValue(typeof(Color), "18,150,219")]
        public Color DownlineFouseColor
        {
            get => this.downlineFouseColor;
            set => downlineFouseColor = value;
        }
        private Color downlineFouseColor = Color.FromArgb(18, 150, 219);


        [Description("编辑框下划线默认色")]
        [DefaultValue(typeof(Color), "Gray")]
        public Color DownlineColor
        {
            get => this.downlineColor;
            set => downlineColor = value;
        }
        private Color downlineColor = Color.Gray;

        /// <summary>
        /// 获取或设置一个值，该值指示是否在文本 System.Windows.Forms.TextBox 控件应显示为默认的密码字符。
        /// </summary>
        /// <returns>true 如果在文本 System.Windows.Forms.TextBox 控件应显示为默认的密码字符; 否则为 false。</returns>
        [DefaultValue(false)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [Category("外观")]
        [Description("获取或设置一个值，该值指示是否在文本 System.Windows.Forms.TextBox 控件应显示为默认的密码字符。")]
        public bool UseSystemPasswordChar
        {
            get => this.textBox1.UseSystemPasswordChar;
            set => this.textBox1.UseSystemPasswordChar = value;
        }

        /// <summary>
        /// 失去焦点时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox1_LostFocus(object sender, EventArgs e)
        {
            color = this.downlineColor;
            this.Invalidate();
        }

        private Color color = Color.Gray;
        /// <summary>
        /// 获取焦点时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextBox1_GotFocus(object sender, EventArgs e)
        {
            color = this.downlineFouseColor;
            this.Invalidate();
        }

        private void MyTextBox_Paint(object sender, PaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(0, 0, this.Width, this.Height);
            int y = rectangle.Y + rectangle.Height;
            e.Graphics.DrawLine(new Pen(color), rectangle.X, y - 1, rectangle.X + rectangle.Width, y - 1);
        }

        public void ResetPasswordChar()
        {
            this.PasswordChar = default;
        }

        public bool ShouldSerializePasswordChar()
        {
            return this.PasswordChar != default;
        }
    }
}
